package com.ybzl.mini.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ybzl.common.core.controller.BaseController;
import com.ybzl.common.core.controller.IdGenerator;
import com.ybzl.common.core.domain.ResultVo;
import com.ybzl.common.core.page.PageData;
import com.ybzl.common.utils.SecurityUtils;
import com.ybzl.mini.entity.MiniBanners;
import com.ybzl.mini.entity.MiniGood;
import com.ybzl.mini.mapper.MiniBannersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/*
 * Desc：一意见反馈
 * @Author jiaxin
 * @Company asiainfo.com
 * @Since  2024-06-29 10:44:56.540
 *
 */

@Slf4j
@RestController
@RequestMapping("/miniBanners")
public class MiniBannersController extends BaseController {

    @Autowired
    private MiniBannersMapper bannersMapper;



    /**
     * 查询 MiniGood 列表，模糊查询
     *
     * @return data data
     */
    @PostMapping("/list")
    public PageData list(@RequestBody MiniBanners banner) {
        startPage();
        LambdaQueryWrapper<MiniBanners> query = new LambdaQueryWrapper<>();
        query.orderByAsc(MiniBanners::getSort);
        List<MiniBanners> banners = bannersMapper.selectList(query);
        return getDataTable(banners);
    }

    /**
     * 根据主键查询
     *
     * @param keyId keyId
     * @return info info
     */
    @GetMapping("/detail/{keyId}")
    public ResultVo detail(@PathVariable("keyId") String keyId) {
        MiniBanners banners = bannersMapper.selectById(keyId);
        return ResultVo.success(banners);
    }

    @DeleteMapping("/delete/{keyId}")
    public ResultVo delete(@PathVariable("keyId") String keyIds) {
        if (keyIds.indexOf(",") > 0) {
            String[] keyIdArray = keyIds.split(",");
            for (String key : keyIdArray) {
                bannersMapper.deleteById(key);
            }
        }
        bannersMapper.deleteById(keyIds);
        return ResultVo.success();
    }

    /**
     * 新增 MiniWenzhang
     *
     * @param banners miniGood
     * @return data data
     */
    @PostMapping("/add")
    public ResultVo add(@RequestBody MiniBanners banners) {
        if (null == banners.getId()) {
            Long id = IdGenerator.orderId(MiniGood.class);
            banners.setId(id);
            banners.setCreateTime(new Date());
            banners.setCreateUser(SecurityUtils.getUsername());
            bannersMapper.insert(banners);
        } else {
            banners.setUpdateTime(new Date());
            banners.setUpdateUser(SecurityUtils.getUsername());
            bannersMapper.updateById(banners);
        }
        return ResultVo.success();
    }

    /**
     *
     * @retur data data
     */
    @PutMapping("/update")
    public ResultVo update(@RequestBody MiniBanners banners) {
        int a = bannersMapper.updateById(banners);
        if(a>0){
            return ResultVo.success();
        }
        return ResultVo.error();
    }
}
